package jp.co.ikd.swing.compornent;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.text.JTextComponent;

public class FieldListener implements FocusListener {

	private IMEType imeMode;
	private JTextComponent component;

	public FieldListener(IMEType imeMode, JTextComponent component) {
		this.imeMode = imeMode;
		this.component = component;
	}

	/**
	 * focusGained時の処理です。
	 * <p>
	 * ・文字列を全選択します。<br>
	 * ・変換モードにIM_NONE以外を指定している場合、IMの制御を行います。
	 */
	@Override
	public void focusGained(FocusEvent e) {

		Character.Subset[] subsets = null;

		if (imeMode == null) {
			return;
		}

		switch (imeMode) {
		case IME_FULL_ASCII:
			subsets = new Character.Subset[] { java.awt.im.InputSubset.FULLWIDTH_LATIN };
			break;
		case IME_HALF_KATAKANA:
			subsets = new Character.Subset[] { java.awt.im.InputSubset.HALFWIDTH_KATAKANA };
		case IME_HIRAGANA:
			subsets = new Character.Subset[] { java.awt.im.InputSubset.KANJI };
		default:
			break;
		}
		this.component.getInputContext().setCharacterSubsets(subsets);

		this.component.select(0, this.component.getText().length());
	}

	/**
	 * focusLost時の処理です。
	 * <p>
	 * ・変換モードにIM_NONE以外を指定している場合、IMの制御を行います。
	 */
	@Override
	public void focusLost(FocusEvent e) {
		if (imeMode != null) {
			this.component.getInputContext().setCharacterSubsets(null);
		}
	}
}
